package com.sfh.cp.mass.pst.impl;

import com.sfh.cp.mass.pst.IPstMass;
import com.sfh.lib.AppCacheManager;
import com.sfh.lib.mvp.service.AbstractPresenter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import crm.sfh.server.SMass;
import crm.sfh.server.entity.EMass;
import crm.sfh.server.entity.req.ReqPage;
import crm.sfh.server.entity.rsp.TPage;

/**
 * 社团列表
 *
 * @date 2018/4/10
 */
public class PstMass extends AbstractPresenter<IPstMass.IVMass> implements IPstMass {

    @Override
    public void listPageMass(final ReqPage req) {

        BmobQuery<SMass> query = new BmobQuery<>();
        query.setSkip((req.nowPage - 1) * 10);
        query.setLimit(req.nowPage * 10);
        query.order("-size");
        query.findObjects(AppCacheManager.getApplication(), new FindListener<SMass>() {
            @Override
            public void onSuccess(List<SMass> list) {
                TPage<EMass> page = new TPage<>();
                page.setNowPage(req.nowPage);
                page.setTotalPage( list.size() >= 10 ? req.nowPage + 1 : req.nowPage);
                List<EMass> masses = new ArrayList<>(list.size());
                for (SMass mass : list){
                    masses.add(mass.getEMass());
                }
                page.setList(masses);
                getView().onSuccess(page);
            }

            @Override
            public void onError(int i, String s) {
                getView().onError("", s);
            }
        });
    }

}
